package com.qianzhou.page.controller;

import com.qianzhou.common.pojo.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: qian.zhou
 * @Description: PageController
 * @Date Created in 2021-09-12-11:29
 * @Modified By:
 */
@RestController
@RequestMapping("/page")
public class PageController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/getData/{id}")
    public Products findDataById(@PathVariable("id") Integer id) {
        Products products = restTemplate.getForObject("http://localhost:9000/product/query/" + id, Products.class);
        System.out.println("从service-product中获得Product对象：" + products);
        return products;
    }

    /**
     * 功能描述 通过RestTemplate和DiscoveryClient的方式调用 -- 引入Eureka注册中中心的概念
     * 从Eureka注册中心中获取地址，获取到实例的信息，然后用restTemplate请求和访问实例
     *
     * @param id
     * @return com.qianzhou.common.pojo.Products
     * @author qian.zhou
     * @date 2021/9/12
     */
    @RequestMapping("query1/{id}")
    public Products query1(@PathVariable("id") Integer id) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int i = host.indexOf(":");
        String hostIP = host.substring(0, i);
        int port = instance.getPort();
        String url = "http://" + hostIP + ":" + port + "/product/query/" + id;
        Products products = restTemplate.getForObject(url, Products.class);
        System.out.println(products);
        return products;
    }

    /**
     * 功能描述 获取Eureka元数据信息
     *
     * @param
     * @return
     * @author qian.zhou
     * @date 2021/9/12
     */
    @RequestMapping("/getMetaData")
    public String getMetaData() {
        StringBuilder result = new StringBuilder();
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        Map<String, String> metadata = instance.getMetadata();
        Set<Map.Entry<String, String>> entries = metadata.entrySet();
        for (Map.Entry<String, String> entry : entries
        ) {
            String key = entry.getKey();
            String value = entry.getValue();
            result.append("key:").append(key).append(",value:").append(value).append("\n");
        }
        return String.valueOf(result);
    }

    /**
     * 功能描述 通过Ribbon负载均衡的方式获取到实例的信息
     *
     * @param
     * @return
     * @author qian.zhou
     * @date 2021/9/12
     */
    @GetMapping("/getPort")
    public String getProductServerPort() {
        String url = "http://service-product/service/port/";
        return restTemplate.getForObject(url,String.class);
    }
}
